package com.example.androidjpbc;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.ConnES.Cipher;
import com.example.ConnES.parseXml;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private static final String TAG="MainActivity";
    private OkHttpClient okHttpClient;
    Pairing pairing = PairingFactory.getPairing("assets/a.properties");

    Element g;
    Element z;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        okHttpClient=new OkHttpClient();

        Button genG=(Button) findViewById(R.id.genG);
        Button genZ=(Button) findViewById(R.id.genZ);
        Button culPow=(Button) findViewById(R.id.pow);
        Button culHash=(Button) findViewById(R.id.hash);
        Button csptime=(Button)findViewById(R.id.csptime);
        genG.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long s = System.currentTimeMillis();
                g= pairing.getG1().newRandomElement();
                long e = System.currentTimeMillis();
                long t1=e-s;
               TextView tv=findViewById(R.id.randomG);
               tv.setText(g.toString());
               TextView timer=findViewById(R.id.time1);
               timer.setText(String.valueOf(t1));

            }
        });
        genZ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long s = System.currentTimeMillis();
               z = pairing.getZr().newRandomElement();
                long e = System.currentTimeMillis();
                long t2=e-s;
               TextView tv=findViewById(R.id.randomZ);
               tv.setText(z.toString());
                TextView timer=findViewById(R.id.time2);
                timer.setText(String.valueOf(t2));
            }
        });

        culPow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv=(TextView)findViewById(R.id.powres);
                long s = System.currentTimeMillis();
               Element powRes = g.powZn(z);
                long e = System.currentTimeMillis();
                long t3=e-s;
                tv.setText( powRes.toString());
                TextView timer=findViewById(R.id.time3);
                timer.setText(String.valueOf(t3));
            }
        });
        culHash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=g.toString();
                long s = System.currentTimeMillis();
                Element res=pairing.getG1().newElement().setFromHash(str.getBytes(), 0, str.length()).getImmutable();
                long e = System.currentTimeMillis();
                long t4=e-s;
                TextView tv=findViewById(R.id.hashres);
                tv.setText(res.toString());
                TextView timer=findViewById(R.id.time4);
                timer.setText(String.valueOf(t4));
            }
        });


        csptime.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View v) {
                Cipher cp = parseXml.sendrequestWithOkHttp();
                TextView enc=(TextView) findViewById(R.id.enc);
                TextView dec=(TextView) findViewById(R.id.dec);
                try{
                    enc.setText(String.valueOf(cp.getEnctime()));
                    dec.setText(String.valueOf(cp.getDectime()));
                }catch(Exception e){
                    Log.i(TAG,"error"+e);
                }



            }
        });

    }

    public void httpTest(View view) {
        parseXml.sendpost();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                Request request = new Request.Builder().url("http://10.206.6.31:8080/crypto/upload").build();
//                Call call = okHttpClient.newCall(request);
//                try {
//                    Response re = call.execute();
//                    Log.i(TAG,"body:"+re.body().string());
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
    }
}